package com.tomorrow.runner.data.model;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.tomorrow.runner.data.AbsData;
import com.tomorrow.runner.data.DatabaseHelper;
import com.tomorrow.runner.data.RunProvider;

import java.util.HashSet;

import cn.sucun.android.utils.SQLUtility;

/**
 *
 */
public class UserModel extends AbsData {

    public static final String DATABASE = "user.db";
    public static final int DATABASE_VERSION = 1;

    public static final String TABLE_NAME = "user_info";
    public static final String CONTENT_NAME = "user";

    public static final String USERNAME = "username";

    public static final String PASSWORD = "password";

    public static final String NICKNAME = "nickname";

    public static final String GANDER = "gander";

    public static final String BIRTHDAY = "birthday";

    public static final String WEIGHT = "weight";

    public static final String TALL = "tall";

    protected static final HashSet<String> COLUMNS_INT;
    protected static final HashSet<String> COLUMNS_LONG;
    protected static final HashSet<String> COLUMNS_STR;
    protected static final HashSet<String> COLUMNS_BOOL;
    protected static final HashSet<String> COLUMNS_DOUBLE;

    static {
        COLUMNS_INT = new HashSet<>();
        COLUMNS_LONG = new HashSet<>();
        COLUMNS_STR = new HashSet<>();
        COLUMNS_BOOL = new HashSet<>();
        COLUMNS_DOUBLE = new HashSet<>();

        COLUMNS_INT.add(_ID);

        COLUMNS_STR.add(USERNAME);
        COLUMNS_STR.add(PASSWORD);
        COLUMNS_STR.add(NICKNAME);


        COLUMNS_BOOL.add(GANDER);

        COLUMNS_LONG.add(BIRTHDAY);

        COLUMNS_DOUBLE.add(WEIGHT);
        COLUMNS_DOUBLE.add(TALL);
    }

    public UserModel(Cursor c) {
        super(c, true, COLUMNS_STR, COLUMNS_INT, COLUMNS_LONG, COLUMNS_BOOL,COLUMNS_DOUBLE);
    }

    private static Uri sContentUri = null;
    /**
     * 获取数据库操作的uri
     * @return 数据库操作的uri
     */
    public static Uri getContentUri() {
        if (sContentUri == null) {
            sContentUri = Uri.withAppendedPath(RunProvider.getContentUri(),CONTENT_NAME);
        }
        return sContentUri;
    }
    @Override
    protected Uri getBaseUri() {
        return getContentUri();
    }

    /**
     * 通过帮助类继承SQLiteOpenHelper的帮助类对象 创建数据库
     */
    public static final DatabaseHelper.DBBuilder BUILDER = new DatabaseHelper.AbsDBBuilder(TABLE_NAME) {

        @Override
        public void createTable(Context context, SQLiteDatabase db) {
            StringBuilder sb = new StringBuilder();
            sb.append(_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
            sb.append(USERNAME).append(" TEXT NOT NULL UNIQUE, ");

            sb.append(PASSWORD).append(" TEXT NOT NULL, ");
            sb.append(NICKNAME).append(" TEXT, ");

            sb.append(GANDER).append(" BOOLEAN NOT NULL DEFAULT 0, ");

            sb.append(BIRTHDAY).append(" LONG , ");
            sb.append(WEIGHT).append(" DOUBLE , ");
            sb.append(TALL).append(" DOUBLE ");

            SQLUtility.createTable(db, TABLE_NAME, sb.toString());
        }

        @Override
        public boolean updateTable(SQLiteDatabase db, int oldVersion, int newVersion) {
            return newVersion > oldVersion;
        }
    };
}
